import 'package:book_app/feature/loginregister/forget_view.dart';
import 'package:book_app/feature/loginregister/register_view.dart';
import 'package:book_app/feature/loginregister/viewmodel/login_view_model.dart';
import 'package:book_app/product/base/base_view.dart';
import 'package:flutter/material.dart';
import 'package:book_app/product/constants/app_colors.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../main/main_page.dart';

class LoginView extends StatefulWidget {
  const LoginView({super.key});

  @override
  State<LoginView> createState() => _LoginViewState();
}

class _LoginViewState extends State<LoginView> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return BaseView<LoginViewModel>(
      viewModel: LoginViewModel(),
      onPageBuilder: (BuildContext context, LoginViewModel viewModel) =>
          Scaffold(
        extendBodyBehindAppBar: true,
        appBar: buildAppbar(),
        body: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/appbackground.png'),
              // 替换为你的背景图片路径
              fit: BoxFit.cover,
            ),
          ),
          child: SafeArea(
            child: ListView(
              children: [
                const SizedBox(
                  height: 50,
                ),
                const Image(
                  image: AssetImage('assets/images/acteclogo.png'),
                  width: 140,
                  height: 45,
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 30, left: 34, right: 34),
                  child: TextField(
                    style: const TextStyle(fontSize: 17, color: Colors.white),
                    decoration: InputDecoration(
                      hintText: '请输入邮箱',
                      hintStyle:
                          TextStyle(color: AppColors.text204, fontSize: 16),
                      enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: AppColors.white33)),
                      focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: AppColors.orange)),
                    ),
                    cursorColor: AppColors.orange,
                    onChanged: (text) {
                      // 这个回调会在用户输入文本时触发
                      // 'text' 参数包含了用户输入的文本
                      viewModel.username = text;
                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 10, left: 34, right: 34),
                  child: TextField(
                    style: const TextStyle(fontSize: 17, color: Colors.white),
                    decoration: InputDecoration(
                      hintText: '请输入密码',
                      hintStyle:
                          TextStyle(color: AppColors.text204, fontSize: 16),
                      enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: AppColors.white33)),
                      focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: AppColors.orange)),
                    ),
                    cursorColor: AppColors.orange,
                    onChanged: (text) {
                      // 这个回调会在用户输入文本时触发
                      // 'text' 参数包含了用户输入的文本
                      viewModel.password = text;
                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 30, left: 34, right: 34),
                  child: ElevatedButton(
                    onPressed: () {
                      _onPressLogin(viewModel, context);
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColors.orange,
                      foregroundColor: Colors.white,
                      minimumSize: const Size(0, 45),
                      //change text color of button
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                    ),
                    child: const Text(
                      '登陆',
                      style:
                          TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                    ),
                  ),
                ),
                Padding(
                    padding:
                        const EdgeInsets.only(top: 10, left: 34, right: 34),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        // 将按钮放在行的两侧
                        TextButton(
                          onPressed: () {
                            // 左边按钮的操作
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => const RegisterView(),
                              ),
                            );
                          },
                          child: Text(
                            '立即注册',
                            style: TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.bold,
                                color: AppColors.orange),
                          ),
                        ),
                        TextButton(
                          onPressed: () {
                            // 左边按钮的操作
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => const ForgetView(),
                              ),
                            );
                          },
                          child: const Text(
                            '忘记密码？',
                            style: TextStyle(fontSize: 15, color: Colors.white),
                          ),
                        ),
                      ],
                    )),
              ],
            ),
          ),
        ),
      ),
      onModelReady: (model) {
        model.setContext(context);
      },
    );
  }

  AppBar buildAppbar() {
    return AppBar(
      backgroundColor: AppColors.transparent,
      elevation: 0,
      centerTitle: true,
    );
  }

  Future<void> _onPressLogin(
      LoginViewModel viewModel, BuildContext context) async {
      bool isLogin = await viewModel.login();
  }
}
